package com.sikaryofficial.knowledge.loader;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.sikaryofficial.knowledge.domain.entity.Person;
import com.sikaryofficial.knowledge.repository.PersonRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

/**
 * @author : qinjinyuan
 * @desc : 加载人员信息
 * @date : 2023/11/29 9:28
 */
@Component
@Slf4j
public class PersonLoader implements CommandLineRunner {

    @Autowired
    private PersonRepository personRepository;
    /**
     * @param args
     * @throws Exception
     */
    @Override
    public void run(String... args) {
        personRepository.deleteAll();

        Person greg = new Person(IdWorker.getId(),"Greg");
        Person roy = new Person(IdWorker.getId(),"Roy");
        Person craig = new Person(IdWorker.getId(),"Craig");

        Person craig1 = new Person(IdWorker.getId(),"Craig1");

        Person craig2 = new Person(IdWorker.getId(),"Craig2");

        Person craig3 = new Person(IdWorker.getId(),"Craig3");

        Person craig4 = new Person(IdWorker.getId(),"Craig4");

        List<Person> team = Arrays.asList(greg, roy, craig);

        log.info("Before linking up with Neo4j...");

        team.forEach(person -> log.info("\t" + person.toString()));

        personRepository.save(greg);
        personRepository.save(roy);
        personRepository.save(craig);

        personRepository.save(craig1);
        personRepository.save(craig2);
        personRepository.save(craig3);
        personRepository.save(craig4);


        greg = personRepository.findByName(greg.getName());
        greg.worksWith(roy);
        greg.worksWith(craig);
        personRepository.save(greg);

        roy = personRepository.findByName(roy.getName());
        roy.worksWith(craig);
        // We already know that roy works with greg
        personRepository.save(roy);

        // We already know craig works with roy and greg
        log.info("Lookup each person by name...");
        team.forEach(person ->
                log.info(
                "\t" + personRepository.findByName(person.getName()).toString()));

        List<Person> teammates = personRepository.findByTeammatesName(greg.getName());
        log.info("The following have Greg as a teammate...");
        teammates.forEach(person -> log.info("\t" + person.getName()));

    }
}
